using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using WindowsPhoneMVC.Phone.Interfaces;

namespace WindowsPhoneMVC.Phone
{
    [DataContract]
    public class PageTransientStore : IPageTransientStore
    {
        public PageTransientStore()
        {
            State = new Dictionary<string, object>();
        }

        [DataMember]
        public Dictionary<string, object> State { get; set; }

        public void Save<T>(string key, T value) where T : class
        {
            State[key] = value;
        }

        public T Load<T>(string key) where T : class
        {
            object value;
            if (State.TryGetValue(key, out value))
                return (T)value;

            return default(T);
        }

        public bool Contains(string key)
        {
            return State.ContainsKey(key);
        }

        public void Remove(string key)
        {
            if (State.ContainsKey(key))
                State.Remove(key);
        }
    }
}